package com.xinyuan.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.xinyuan.entity.*;
import com.xinyuan.service.AccessWork;
import com.xinyuan.utils.AmecoUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Classname ${NAME}
 * @Description TODO
 * @Date 2020/6/1 18:17
 * @Created by zfl
 */
@WebServlet(name = "UpdateWorkHour",urlPatterns = "/updateWorkHourSchemeOne")
public class UpdateWorkHourSchemeOne extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.addHeader("Access-Control-Allow-Origin","*");
        String fileID = request.getParameter("id");
        String fileName = "WorkHourSchemeOne.xls";
        String filePath = "C:\\upload\\" + fileID + "\\MTOPONE.xlsx";
        double totalWorkHour = 0.0,totalAccessHour = 0.0,totalHour = 0.0;
        AmecoUtils utils = new AmecoUtils();
        List<CheckMessage> list = new ArrayList<CheckMessage>();
        list = utils.getWorkHour(filePath);
        String str = utils.acceptJSON(request);
        ObjectMapper mapper = new ObjectMapper();
        TimeType timeType = mapper.readValue(str, TimeType.class);
        Total total = utils.countWorkHour(list, timeType);
        AccessWork accessWork = utils.designExcel(fileID, total, timeType,fileName,filePath);
        System.out.println(accessWork);
        RespWork respWork = new RespWork();

        //总工作时间
        //A检工作时长
        totalWorkHour = Math.round(total.getATotalHour() * 1000) / 1000.00;
        respWork.setTotalAWorkHour(totalWorkHour);
        //C检工作时长
        totalWorkHour = Math.round(total.getCTotalHour() * 1000) / 1000.00;
        respWork.setTotalCWorkHour(totalWorkHour);

        //总接近工时
        //A检接近时长
        totalAccessHour = Math.round(accessWork.accessTimeATotal * 1000) / 1000.00;
        respWork.setTotalAAccessHour(totalAccessHour);
        //C检接近时长
        totalAccessHour = Math.round(accessWork.accessTimeCTotal * 1000) / 1000.0;
        respWork.setTotalCAccessHour(totalAccessHour);
        //A总时长
        totalHour = respWork.getTotalAAccessHour() + respWork.getTotalAWorkHour();
        totalHour = Math.round(totalHour * 1000) / 1000.00;
        respWork.setTotalAHour(totalHour);
        //C检总时长
        totalHour = respWork.getTotalCAccessHour() + respWork.getTotalCWorkHour();
        totalHour = Math.round(totalHour * 1000) / 1000.00;
        respWork.setTotalCHour(totalHour);
        List<Work> listA = new ArrayList<>();
        List<Work> listC = new ArrayList<>();
        //计算A检
        double count_A = timeType.getATime().getYear() * 365 / timeType.getATime().getValue();
        count_A = (int) Math.floor(count_A);
        //计算C检
        double count_C = timeType.getCTime().getYear() * 365 / timeType.getCTime().getValue();
        count_C = (int) Math.floor(count_C);
        for (int i = 1; i <= count_A; i++) {
            Work work = new Work();
            work.setNumber(i);
            work.setWorkTime(Math.round(total.getAmap().get(i) * 1000) / 1000.00);
            work.setAccessTime(Math.round(accessWork.accessTimeMapA.get(i) * 1000) / 1000.00);
            listA.add(work);
        }
        respWork.setAwork(listA);

        for (int i = 1; i <= count_C; i++) {
            Work work = new Work();
            work.setNumber(i);
            work.setWorkTime(Math.round(total.getCmap().get(i) * 1000) / 1000.00);
            work.setAccessTime(Math.round(accessWork.accessTimeMapC.get(i) * 1000) / 1000.00);
            listC.add(work);
        }
        respWork.setCwork(listC);
        respWork.setMsg("成功");
        respWork.setErrCode(0);
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(utils.getJson(respWork));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
